class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
    {
        if(root==nullptr) return {};
        vector<vector<int>> res;
        map<int,vector<int>> m;
        bfs(root,m,0);
        for(auto it:m) res.push_back(it.second);
        return res;
    }
    void bfs(TreeNode* root,map<int,vector<int>> &m,int index)
    {
        if(root==nullptr) return;
        m[index].push_back(root->val);
        bfs(root->left,m,index+1);
        bfs(root->right,m,index+1);
    }
};